package com.tianyadu.mmall.core.validate.code.service.impl;

import com.tianyadu.mmall.core.conf.SecurityConstantsConfig;
import com.tianyadu.mmall.core.properties.MallSecurityProperties;
import com.tianyadu.mmall.core.validate.code.ImageCode;
import com.tianyadu.mmall.core.validate.code.service.ValidateCodeGenerator;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * 校验码生成器
 *
 * @author xueyj
 * @date 2019-11-12 21:43
 */
@Component
public class ImageValidateCodeGenerator implements ValidateCodeGenerator {

    /**
     * 安全认证配置
     */
    @Autowired
    private MallSecurityProperties securityProperties;

    /**
     * 生成图形校验码
     *
     * @param request
     * @return com.tianyadu.mmall.core.validate.code.image.ImageCode
     * @author xueyj
     * @date 2019/11/12-21:45
     */
    @Override
    public ImageCode generate(@NotNull ServletWebRequest request) {
        //  验证码宽度
        int imageCodeWidth = securityProperties.getCode().getImage().getWidth();
        //  验证码高度
        int imageCodeHeight = securityProperties.getCode().getImage().getHeight();
        //  验证码长度（位数）
        int imageCodeLength = securityProperties.getCode().getImage().getLength();
        //  验证码过期时间
        int imageCodeExpireIn = securityProperties.getCode().getImage().getExpireIn();
        //  验证码宽度
        String validateCodeWidth = SecurityConstantsConfig.VALIDATE_CODE_WIDTH;
        //  验证码高度
        String validateCodeHeight = SecurityConstantsConfig.VALIDATE_CODE_HEIGHT;
        HttpServletRequest servletRequest = request.getRequest();
        //	图片宽度
        int width = ServletRequestUtils.getIntParameter(servletRequest, validateCodeWidth, imageCodeWidth);
        //	图片高度
        int height = ServletRequestUtils.getIntParameter(servletRequest, validateCodeHeight, imageCodeHeight);
        //  构建图片信息
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();
        //  构建图片干扰条纹
        Random random = new Random();

        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.ITALIC, 20));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }

        //	生成四位随机数验证码
        String sRand = "";
        for (int i = 0; i < imageCodeLength; i++) {
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            //  将随机数写入图片
            g.drawString(rand, 13 * i + 6, 16);
        }

        g.dispose();
        return new ImageCode(image, sRand, imageCodeExpireIn);
    }

    /**
     * 生成随机背景条纹
     *
     * @param fc
     * @param bc
     * @return
     */
    private Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    public MallSecurityProperties getSecurityProperties() {
        return securityProperties;
    }

    public void setSecurityProperties(MallSecurityProperties securityProperties) {
        this.securityProperties = securityProperties;
    }
}